﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

public partial class frmLogin :BaseWinFrom
{
    public frmLogin()
    {
        InitializeComponent();
    }

    private void btLogin_Click(object sender, EventArgs e)
    {
        RST.Certificate rst = new RST.Certificate();

        this.btLogin.Enabled = false;

        System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(HandleLogin), new Model.Certificate
        {
            Name = txtUserName.Text,
            Password = txtPassword.Text,

        });
    }

    void HandleLogin(object s)
    {
        if (s != null || s.GetType() == typeof(Model.Certificate))
        {
            RST.Certificate rsp = new RST.Certificate();
            rsp.Login(s as Model.Certificate);
            Invoke(new HandledEventHandler(HandleLoginInvoke), new object[] { s, null });
        }
    }
    
    void HandleLogin(object s, EventArgs e)
    {
        if (s != null || s.GetType() == typeof(Model.Certificate))
        {
            RST.Certificate rsp = new RST.Certificate();
            rsp.Login(s as Model.Certificate);
            Invoke(new HandledEventHandler(HandleLoginInvoke), new object[] { s, null });
        }
    }

    void HandleLoginInvoke(object s, EventArgs e)
    {
        Model.Certificate c = s as Model.Certificate;
        if (c != null && c.Id != null)
        {
            Core.Session.Certificate = c;
            new frmMain().Show();
            this.Dispose();
        }
        this.btLogin.Enabled = true;
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        Application.Exit();
    }
}